package com.gzc.pm.logic
{
	import cn.skyclass.business.SessionManager;
	import cn.skyclass.controls.AdvancedAlert;
	import cn.skyclass.net.BaseSubmitter;
	import cn.skyclass.net.OperationResult;
	
	import com.gzc.pm.bean.AlarmRuleBean;
	import com.gzc.pm.dao.AlarmRuleDao;
	
	import mx.rpc.events.ResultEvent;
	
	public class AlarmRuleEditor extends BaseSubmitter
	{
		public function AlarmRuleEditor(alarmRule:AlarmRuleBean)
		{
			_alarmRule = alarmRule;
		}
		
		private var _alarmRule:AlarmRuleBean;
		
		override public function validate():String{
			return null;
		}
		
		override protected function doSubmit():void{
			var dao:AlarmRuleDao = new AlarmRuleDao();
			if(_alarmRule.id){
				dao.updateByUserId(_alarmRule, onEdited);
			}else{
				dao.insert(_alarmRule, onAdded);
			}
		}
		
		private function onEdited(e:ResultEvent):void{
			var result:OperationResult = e.result as OperationResult;
			if(result.successful){
				SessionManager.current.setAttribute('alarmRule', _alarmRule);
				dispatchSubmittedEvent();
			}else{
				AdvancedAlert.info('设置告警规则失败！');
				dispatchFailedEvent();
			}
		}
		
		private function onAdded(e:ResultEvent):void{
			var result:OperationResult = e.result as OperationResult;
			if(result.successful){
				_alarmRule.id = result.id;
				SessionManager.current.setAttribute('alarmRule', _alarmRule);
				dispatchSubmittedEvent();
			}else{
				AdvancedAlert.info('设置告警规则失败！');
				dispatchFailedEvent();
			}
		}
		
	}
}